﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SMS.Core
{
    /// <summary>
    /// 类型转换
    /// </summary>
    public class TypeConvert
    {
        #region 字符串修改
        /// <summary>
        /// 将字符串填充为8个，不足的前面补0，如10012->00010012
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        public static string BuffZeroToStr(string temp)
        {
            string str = string.Format("{0,8}", temp);
            str = str.Replace(" ", "0");
            return str;
        }
        #endregion

        #region byte->bit_string
        /// <summary>
        /// 将一个字节值转换位对应的二进制8数字的字符串
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        public static string ByteToBit(byte temp)
        {
            string str = Convert.ToString(temp, 2);
            str = string.Format("{0,8}", str);
            str = str.Replace(" ", "0");
            return str;
        }
        #endregion

        #region byte[]->string
        /// <summary>
        /// 将字节数组以16进制字符串的形式显示；如0x1A->"1A"
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        public static string ByteArraytoStr(byte[] temp)
        {
            string str = "";
            for (int i = 0; i < temp.Length; i++)
            {
                str += temp[i].ToString("X2");
            }
            return str;
        }
        #endregion

        #region string->byte
        /// <summary>
        /// 将八位位组转换为字节，如"00010011"->0x13
        /// </summary>
        /// <param name="temp">待转换字符串</param>
        /// <returns>转换后的字节</returns>
        public static byte STR_8toBYTE(string temp)
        {
            int len = temp.Length;
            int i, j;
            int re = 0;
            byte ra;
            string str = "";

            for (i = 0; i < len; i++)
            {
                str = temp.Substring(i, 1);
                ra = Convert.ToByte(str);
                j = len - i - 1;
                re = re | (ra << j);
            }
            ra = (byte)re;
            return ra;
        }
        /// <summary>
        /// 将八位位组转换为字节，如"00010011"->0x13
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        public static byte Str_8ToByte(string temp)
        {
            byte a = Convert.ToByte(temp, 2);
            return a;
        }
        /// <summary>
        /// 将16进制字符串转换为字节，如"1A"->0x1a
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        public static byte STR_2toBYTE(string temp)
        {
            string str = "";
            byte[] b = new byte[2];
            byte da;
            int i;
            for (i = 0; i < 2; i++)
            {
                str = temp.Substring(i, 1);
                switch (str)
                {
                    case "0":
                        b[i] = 0x00;
                        break;
                    case "1":
                        b[i] = 0x01;
                        break;
                    case "2":
                        b[i] = 0x02;
                        break;
                    case "3":
                        b[i] = 0x03;
                        break;
                    case "4":
                        b[i] = 0x04;
                        break;
                    case "5":
                        b[i] = 0x05;
                        break;
                    case "6":
                        b[i] = 0x06;
                        break;
                    case "7":
                        b[i] = 0x07;
                        break;
                    case "8":
                        b[i] = 0x08;
                        break;
                    case "9":
                        b[i] = 0x09;
                        break;
                    case "A":
                        b[i] = 0x0a;
                        break;
                    case "B":
                        b[i] = 0x0b;
                        break;
                    case "C":
                        b[i] = 0x0c;
                        break;
                    case "D":
                        b[i] = 0x0d;
                        break;
                    case "E":
                        b[i] = 0x0e;
                        break;
                    case "F":
                        b[i] = 0x0f;
                        break;
                }
            }

            da = Convert.ToByte(b[0] << 4 | b[1]);
            return da;

        }
        /// <summary>
        /// 将16进制字符串转换为字节，如"1A"->0x1a
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        public static byte Str_2toByte(string temp)
        {
            byte a = Convert.ToByte(temp, 16);
            return a;
        }
        #endregion

        #region string->byte[]
        /// <summary>
        /// 将16进制字符串转换为字节，如1A2B3C->0x1A,0x2B,0x3C
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        public static byte[] Str_2toByteArray(string temp)
        {
            int i, j;
            int len = temp.Length;
            byte[] bytes = new byte[len / 2];
            j = 0;
            for (i = 0; i < len; i += 2)
            {
                bytes[j] = Convert.ToByte(temp.Substring(i, 2), 16);

                j++;
            }
            return bytes;

        }
        #endregion

        #region string->int
        /// <summary>
        /// 将八位位组转换为字节，如"00010011"->0x13
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        public static int Str_8ToInt(string temp)
        {
            int a = Convert.ToInt32(temp, 2);
            return a;
        }

        /// <summary>
        /// 将16进制字符串转换为字节，如"1A"->0x1a
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        public static int Str_2toInt(string temp)
        {
            int a = Convert.ToInt32(temp, 16);
            return a;
        }

        #endregion
    }
}
